package com.zqx.kafkaproject.kafkademo.components;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zqx.kafkaproject.kafkademo.dataobject.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Component
@Slf4j
public class KafkaSender {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    public void send()
    {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        /*message.setMsg(UUID.randomUUID().toString());*/
        message.setMsg("测试kafka");
        message.setSendTime(new Date());
        log.info("message={}",gson.toJson(message));
        log.info("【数据发送成功】");
        kafkaTemplate.send("zhisheng",gson.toJson(message));
    }
}
